﻿using Furion.Logging;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using StackExchange.Profiling.Internal;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using TMV.PrintServer.Comm;
using TMV.PrintServer.Model;
using static System.Drawing.Printing.PrinterSettings;

namespace TMV.PrintServer
{
    public class PrintHelp
    {
		string msg = "";
		public PrintHelp()
		{
			//dbContext= _dbContext;
		}
		public void BeginPtint(PrintDto receiveData)
		{
			WordUtil wordUtil = new();
			var strm = wordUtil.WordWrite(receiveData);
			Print(strm);
		}
		public void Print(MemoryStream memStream)
        {
			Document doc = new Document();
			try
            {
                StringCollection strList = PrinterSettings.InstalledPrinters;
                var list = strList.Cast<string>().ToList();
                doc.LoadFromStream(memStream,FileFormat.Docx);
                //Deli DB-615KI
                if (list.Any(x => x == "Deli DB-615KII"))
                {
                    ///后台打印
                    PrintDocument pd = doc.PrintDocument;
                    pd.PrinterSettings.PrinterName = "Deli DB-615KII";
                    pd.PrinterSettings.DefaultPageSettings.Landscape = true;
                    pd.DefaultPageSettings.PaperSize = new PaperSize("aa", 800, 366);
                    pd.OriginAtMargins = true;
					System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(20, 50, 0, 0);
                    pd.DefaultPageSettings.Margins = margins;
					//添加印章图片

					Image image = Image.FromFile(Application.StartupPath + @"\00.png");

					DocPicture picture = doc.Sections[0].Paragraphs[0].AppendPicture(image);
					//指定印章位置

					picture.HorizontalPosition = 50f;

					picture.VerticalPosition = 60f;
					//设置印章图片大小

					picture.Width = 120;

					picture.Height = 120;
					//设置图片浮于文字上方

					picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
					//pd.PrintPage += new PrintPageEventHandler(this.pdPrint);
					pd.Print();
                    doc.Close();
                }
                else
                {
					Log.Information("请检查打印机是否正常连接");
                }
            }
            finally
            {
                doc.Close();
            }
		}
	}
}
